package com.qhsolution.adapter;

import com.qhsolution.bdict.R;
import com.qhsolution.dbhelper.DataHelper;
import com.qhsolution.object.Word;

import java.util.ArrayList;

import android.app.Activity;
import android.content.ContentValues;
import android.content.SharedPreferences;
import android.preference.PreferenceManager;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;

public class WordArrayAdapter extends ArrayAdapter<Word> {
	Activity context = null;
	ArrayList<Word> myArray = null;
	int layoutId;
	public static final String KEY_ID = "_id";

	public WordArrayAdapter(Activity context, int layoutId, ArrayList<Word> arr) {
		super(context, layoutId, arr);
		this.context = context;
		this.layoutId = layoutId;
		this.myArray = arr;
	}

	public View getView(int position, View convertView, ViewGroup parent) {
		LayoutInflater inflater = context.getLayoutInflater();
		convertView = inflater.inflate(layoutId, null);
		if (myArray.size() > 0 && position >= 0) {
			final TextView txtId = (TextView) convertView
					.findViewById(R.id.txtId);
			final TextView txtDescription = (TextView) convertView
					.findViewById(R.id.txtDescription);
			final TextView txtWordMain = (TextView) convertView
					.findViewById(R.id.txtWordMain);
			final TextView txtWordSub = (TextView) convertView
					.findViewById(R.id.txtWordSub);
			final Word word = myArray.get(position);
			
			txtWordMain.setText(word.getEng());
			txtWordSub.setText(context.getString(R.string.language_vi_text)+": "+word.getName());
			
			if(word.getDescription().length() > 150)
				txtDescription.setText(word.getDescription().substring(0, 150) + "...");
			else
				txtDescription.setText(word.getDescription());
			txtId.setText(String.valueOf(word.getId()));
			final ImageView imgitem = (ImageView) convertView
					.findViewById(R.id.imgFeatured);
			if (word.checkIsFeature())
				imgitem.setImageResource(R.drawable.ic_action_important);
			else
				imgitem.setImageResource(R.drawable.ic_action_not_important);
			imgitem.setOnClickListener(new OnClickListener(){
				@Override
				public void onClick(View arg0) {
					// TODO Auto-generated method stub
					DataHelper dbHelper = new DataHelper(context);
					dbHelper.openDataBase();
					ContentValues value = new ContentValues();
					if (word.checkIsFeature())
					{						
						value.put("featured", "0");
						word.setFeatured(0);
						imgitem.setImageResource(R.drawable.ic_action_not_important);
					}
					else
					{
						value.put("featured", "1");
						word.setFeatured(1);
						imgitem.setImageResource(R.drawable.ic_action_important);
					}
					dbHelper.updateItems("word", value, KEY_ID + " = ? ", new String[]{String.valueOf(word.getId())});
					dbHelper.close();
				}
			});
		}
		return convertView;
	}
}
